username=`whoami`
current_path=`pwd`
devkit_path=`dirname $current_path`
install_dir=/home/$username/.devkit.myenv

function make_sure_dir_ok
{
  if [ ! -d "$1" ]; then
    echo "创建文件夹: $1"
    mkdir -p "$1"
  fi
}

# 安装位置
echo "将会安装在：$install_dir"
make_sure_dir_ok $install_dir

# 数据位置
data_path=$1
if [ "$1" == "" ]; then
    data_path=/home/$username
    echo 'WARNING: 未指定数据目录，默认使用$data_path。重新 sh setup.sh DATAPATH 覆盖即可。'
fi


# 1 拷贝到安装位置
echo "拷贝环境变量配置"
cp $current_path/* $install_dir -fr
rm $install_dir/setup.sh

# 2 拷贝一些工具到安装目录
echo "拷贝一些常用bin"
install_bin_dir="$install_dir/bin"
make_sure_dir_ok $install_bin_dir
cp $devkit_path/debug_tools/pstack-analyzer.py $install_bin_dir/pstack-analyzer
cp $devkit_path/debug_tools/postgresqlconf-operate.py $install_bin_dir/postgresqlconf-operate
cp $devkit_path/dev_tools/buildingblock/occ.py $install_bin_dir/occ
cp $devkit_path/dev_tools/buildingblock/tpcc.py $install_bin_dir/tpcc
chmod +x $install_bin_dir/*

# 3 生成环境变量入口
myenv_source=$install_dir/myenv.source
cat > $install_dir/myenv.source << EOF
  export MYENV=$install_dir
  export DATAPATH=$data_path
  export DEVKIT=$current_path/..
EOF
cat  $current_path/myenv.source.base >> $myenv_source

# 4 生成version文件
version_file=$install_dir/version
git log -n 1 | head -n 3 > $version_file

# 5 注册alias到.bashrc
grep_result=$(grep -F "# MYENV ENV" ~/.bashrc)
if [ "$grep_result" == "" ]; then
    echo "创建环境变量alias快捷方式: me "
    echo "# MYENV ENV" >> ~/.bashrc
    echo "alias me='bash -c source $install_dir/myenv.source'" >> ~/.bashrc
fi

# 6 其他
echo "echo: alias suc='su - carrot' >> /root/.bashrc"
echo "source ~/.bashrc"
echo "finish"




